
#include "mainwindow.h"
#include "httpclient.h"
#include "ui_mainwindow.h"
#include <qjsonobject.h>
#include <qlistwidget.h>
#include <QtWebEngineWidgets>
#include <qd.h>

#define ASSIGN_FROM_VARIANT(field, variant) \
    this->field = variant.value(#field).toString();

#define ASSIGN_FROM_VARIANT_INT(field, variant) \
    this->field = variant.value(#field).toInt();

QString time_to_string(qint64 timestamp) {
    return QDateTime::fromSecsSinceEpoch(timestamp).toString("yyyy-MM-dd hh:mm:ss");
}
qint64 string_to_time(const QString &datetime) {
    return QDateTime::fromString(datetime, "yyyy-MM-dd hh:mm:ss").toSecsSinceEpoch();
}

static QString postList = "https://product.kylinos.cn/api/v1/circle/postList";
struct PostListData {
    int id;
    QString nickName;
    QString postName;
    QString categoryName;
    QString content;
    QString updateTime;

    PostListData& operator=(const PostListData& data);
    PostListData& operator=(const QVariantMap& map);
    PostListData& operator<<(const QVariantMap& map);

    QString post_url() const {
        return QString("https://forum.kylinos.cn/detail?id=%1").arg(this->id);
    }

    PostListData& operator=(const QListWidgetItem& item);
    void operator<<(const QListWidgetItem* item);
    void operator<<(const QListWidgetItem &item);
    void operator>>(QListWidgetItem *item);
    void operator>>(QListWidgetItem& item);
};

PostListData& PostListData::operator=(const PostListData& data) {
    if (this == &data) return *this;

    this->id = data.id;
    this->nickName = data.nickName;
    this->postName = data.postName;
    this->categoryName = data.categoryName;
    this->content = data.content;
    this->updateTime = data.updateTime;
    return *this;
}

PostListData& PostListData::operator=(const QVariantMap& map) {
    ASSIGN_FROM_VARIANT_INT(id, map);
    ASSIGN_FROM_VARIANT(nickName, map);
    ASSIGN_FROM_VARIANT(postName, map);
    ASSIGN_FROM_VARIANT(categoryName, map);
    ASSIGN_FROM_VARIANT(content, map);
    // ASSIGN_FROM_VARIANT(updateTime, map);
    this->updateTime = time_to_string(map.value("updateTime").toLongLong());
    return *this;
}

PostListData& PostListData::operator<<(const QVariantMap& map) {
    return *this = map;
}

PostListData& PostListData::operator=(const QListWidgetItem& item) {
    *this << item;
    return *this;
}

void PostListData::operator<<(const QListWidgetItem* item) {
    *this << *item;
}

void PostListData::operator<<(const QListWidgetItem &item) {
    id           = item.data(990).toInt();
    nickName     = item.data(991).toString();
    postName     = item.data(992).toString();
    categoryName = item.data(993).toString();
    content      = item.data(994).toString();
    updateTime   = item.data(995).toString();
}

void PostListData::operator>>(QListWidgetItem *item) {
    *this >> *item;
}

void PostListData::operator>>(QListWidgetItem& item) {
    item.setData(990, id);
    item.setData(991, nickName);
    item.setData(992, postName);
    item.setData(993, categoryName);
    item.setData(994, content);
    item.setData(995, updateTime);
}


void MainWindow::initPostList()
{
    ui->listWidget->clear();

    auto resp = HttpClient::instance().get(postList);
    auto list = resp.toMap().value("list").toList();

    PostListData post;
    for (auto var: list) {
        post = var.toMap();
        auto item = new QListWidgetItem();
        post >> *item;
        item->setText("[" + post.updateTime + "] " + post.nickName + ": " + post.postName);
        ui->listWidget->addItem(item);
    }
}

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->splitter->setSizes(QList<int>() << 200 << 400);

    connect(ui->listWidget, &QListWidget::itemSelectionChanged, [this](){
      auto item = ui->listWidget->currentItem();
      if (item) {
          PostListData post;
          post << item;
          ui->textEdit->setText(post.content);
          ui->webEngineView->load(post.post_url());
      }
    });
}
MainWindow::~MainWindow()
{
	
}

void MainWindow::on_splitter_splitterMoved(int pos, int index)
{
    qd << pos << index;
}

void MainWindow::showEvent(QShowEvent *event)
{
    initPostList();
}

